<?php

/**
 *      [Discuz!] (C)2001-2099 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: function_seccode.php 6741 2010-03-25 07:36:01Z cnteacher $
 */

define('SECCODE_LIFE', 180);
define('SECQAA_LIFE', 180);

function make_seccode(){
	global $_G;
	$seccode = random(6, 1);
	$seccodeunits = '';
	if($_G['setting']['seccodedata']['type'] == 1) {
		$lang = lang('forum/seccode');
		$len = strtoupper(CHARSET) == 'GBK' ? 2 : 3;
		$code = array(substr($seccode, 0, 3), substr($seccode, 3, 3));
		$seccode = '';
		for($i = 0; $i < 2; $i++) {
			$seccode .= substr($lang['chn'], $code[$i] * $len, $len);
		}
	} elseif($_G['setting']['seccodedata']['type'] == 3) {
		$s = sprintf('%04s', base_convert($seccode, 10, 20));
		$seccodeunits = 'CEFHKLMNOPQRSTUVWXYZ';
	} else {
		$s = sprintf('%04s', base_convert($seccode, 10, 24));
		$seccodeunits = 'BCEFGHJKMPQRTVWXY2346789';
	}
	if($seccodeunits) {
		$seccode = '';
		for($i = 0; $i < 4; $i++) {
			$unit = ord($s{$i});
			$seccode .= ($unit >= 0x30 && $unit <= 0x39) ? $seccodeunits[$unit - 0x30] : $seccodeunits[$unit - 0x57];
		}
	}
	dsetcookie('seccode', md5(strtoupper($seccode)."\t".$_G['config']['security']['authkey']."\t".substr(TIMESTAMP, 0, 7)), SECCODE_LIFE);
	return $seccode;
}

function make_secqaa() {
	global $_G;
	loadcache('secqaa');
	$secqaakey = max(1, random(1, 1));
	$mask = random(5).substr(TIMESTAMP, 0, 7);
	dsetcookie('secqaa', md5($_G['cache']['secqaa'][$secqaakey]['answer']."\t".$_G['config']['security']['authkey']."\t".substr(TIMESTAMP, 0, 7)), SECQAA_LIFE);
	return $_G['cache']['secqaa'][$secqaakey]['question'];
}

?>
